package 力扣日常刷题.木22二月.第17天223;

import java.util.HashMap;

/**
 * @author 帅小伙
 * @date 2022/2/23
 * @description
 */
public class Demo147最长不含重复元素的子字符串 {


    public int lengthOfLongestSubstring(String s) {
        int ans = 0,i = -1;
        HashMap<Character, Integer> map = new HashMap<>();
        for (int j = 0; j < s.length(); j++) {
            if(map.containsKey(s.charAt(j))) {
                i = Math.max(i,map.get(s.charAt(j)));
            }
            map.put(s.charAt(j),j);
            ans = Math.max(ans,j - i);
        }
        return ans;
    }

}
